package com.ruoyi.graduate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.graduate.domain.entity.MsPackageType;
import com.ruoyi.graduate.domain.param.MsPackageTypeAddParam;
import com.ruoyi.graduate.domain.param.MsPackageTypeUpdataParam;
import com.ruoyi.graduate.domain.param.PageBaseParam;
import com.ruoyi.graduate.domain.vo.MsPackageTypeListVo;
import com.ruoyi.graduate.mapping.MsPackageTypeMapping;
import com.ruoyi.graduate.service.IMsPackageTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐类型控制接口
 */
@RestController
@RequestMapping("/api/packagetype")
@Api(tags = "套餐类型控制接口")
public class MsPackageTypeController extends BaseController {

    @Autowired
    private IMsPackageTypeService msPackageTypeService;


    @ApiOperation("套餐类型分页查询")
    @GetMapping
    public TableDataInfo list(PageBaseParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<MsPackageType> list = msPackageTypeService.list();
        PageInfo<MsPackageType> pageInfo = new PageInfo<>(list);

        return getDataTable(pageInfo.getList(), pageInfo.getTotal());
    }


    @ApiOperation("套餐类型新增")
    @PostMapping
    public AjaxResult add(@RequestBody MsPackageTypeAddParam param) {

        return msPackageTypeService.addMsPackageType(param);
    }


    @ApiOperation("套餐类型修改")
    @PutMapping
    public AjaxResult updata(@RequestBody MsPackageTypeUpdataParam param) {

        return msPackageTypeService.updateMsPackageType(param);
    }


    @ApiOperation("套餐类型删除")
    @DeleteMapping("{id}")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult delete(@PathVariable Long id) {

        boolean b = msPackageTypeService.removeById(id);

        return b ? AjaxResult.success() : AjaxResult.error();
    }


    @ApiOperation("套餐类型列表")
    @GetMapping("/list")
    public AjaxResult getList(){

        QueryWrapper qw = new QueryWrapper();
        qw.eq("status",1);
        List<MsPackageType> list = msPackageTypeService.list(qw);
        List<MsPackageTypeListVo> list1 = MsPackageTypeMapping.INSTANCE.getList(list);

        return AjaxResult.success(list1);
    }
}
